﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yto.Common.Extensions;

namespace Yto.Common.Configuration
{
    public class BaseConfig : IBaseConfig
    {

        protected Dictionary<string,object> Settings { get; private set; }

        protected BaseConfig()
        {
            Settings = new Dictionary<string, object>();
        }

        public object this[string name]
        {
            get { return Settings.GetOrDefault(name); }

            set { Settings[name] = value; }
        }


        public T GetOrCreate<T>(string name,Func<T> func)
        {
            var value = Get(name);
            if(value == null)
            {
                value = func();
                Set(name, value);
            }

            return (T)value;
        }

        public object Get(string name)
        {
            return Get(name, null);
        }

        public void Set<T>(string name, T value)
        {
            this[name] = value;
        }

        public object Get(string name, object defaultValue)
        {
            var value = this[name];
            if(value == null)
            {
                return defaultValue;
            }

            return value;
        }

        public T Get<T>(string name, T defaultValue)
        {
            return (T)Get(name, (object)defaultValue);
        }
    }
}
